package com.android.yummycookies.component.loadingScreen;

import java.util.ArrayList;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.yummycookies.R;
import com.android.yummycookies.asyncTask.LoadBitmapThread;
import com.android.yummycookies.asyncTask.ScaleBitmapThread;
import com.android.yummycookies.model.theme.Theme;
import com.android.yummycookies.types.CookieType;

public class SimpleThemeLoadingScreen extends AbstractThemeLoadingScreen {

	public static final int NB_COOKIE_HEIGHT = 10;

	private GridLayout leftColomnCookie;
	private GridLayout rightColomnCookie;

	public SimpleThemeLoadingScreen(Context context, Theme theme,
			int heightToScale, int widthToScale) {
		super(context, theme, heightToScale, widthToScale);

		leftColomnCookie = (GridLayout) findViewById(R.id.loading_screen_left_border);
		rightColomnCookie = (GridLayout) findViewById(R.id.loading_screen_right_border);
	}

	@Override
	public int getLayoutId() {
		return R.layout.activity_simple_theme_loader;
	}

	@Override
	public void createDecoration() {

		int layoutHeight = getMeasuredHeight();
		int cookieHeight = layoutHeight / NB_COOKIE_HEIGHT;

		float ratio = (float) getWidthToScale() / getHeightToScale();

		int cookieWidth = (int) (cookieHeight * ratio);

		int nbCookies = layoutHeight / cookieHeight;

		ArrayList<CookieType> cookieTypes = getTheme().getElemTypes();
		int nbCookieTypes = cookieTypes.size();

		int numCookieType = 0;

		for (int i = 0; i < nbCookies; i++) {
			ImageView ivLeft = new ImageView(getContext());
			ivLeft.setBackgroundResource(cookieTypes.get(numCookieType)
					.getBitmapId());

			ImageView ivRight = new ImageView(getContext());
			ivRight.setBackgroundResource(cookieTypes.get(numCookieType)
					.getBitmapId());

			numCookieType++;
			if (numCookieType == nbCookieTypes) {
				numCookieType = 0;
			}

			GridLayout.LayoutParams param = new GridLayout.LayoutParams();
			param.width = cookieWidth;
			param.height = cookieHeight;
			param.rowSpec = GridLayout.spec(i);
			param.columnSpec = GridLayout.spec(0);
			leftColomnCookie.addView(ivLeft, param);
			rightColomnCookie.addView(ivRight, param);
		}

	}

}
